<template>
  <div id="convertM">
    <div>
      <form @submit.prevent="submit">
        <label class="lab_c" for="sm">小写数字金额：</label>
        <input id="sm" class="in_c" type="text" name="sm" v-model="sm" placeholder="请输入金额">
        <input id = "cb" type="submit" value="转换为大写金额"><br/>
        <label class="lab_c" for="bm">中文大写金额：</label>
        <input id="bm" class="in_c" type="text" name="bm" v-model="bm" readonly="readonly">
      </form>
    </div>
    <div class="explain">
      <h4>注：1、金额具体到“<span class="red-c">分</span>”，例：123.03  壹佰贰拾叁元零叁<span class="red-c">分</span>。</h4>
      <h4>&emsp;&emsp;2、金额不到“分”的以“<span class="red-c">整</span>”结尾，例：123.10 壹佰贰拾叁元壹角<span class="red-c">整</span>。</h4>
      <h4>&emsp;&emsp;3、大写金额要顶格写</h4>
    </div>
  </div>
</template>

<script>

export default {
  name: 'convertM',
  data () {
    return {
      sm: '',
      bm: '',
      submitting: 0
    }
  },
  methods: {
    submit: function () {
      // 汉字的数字
      let cnNums = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
      // 基本单位
      let cnIntRadice = ['', '拾', '佰', '仟']
      // 对应整数部分扩展单位
      let cnIntUnits = ['', '万', '亿', '兆']
      // 对应小数部分单位
      let cnDecUnits = ['角', '分', '毫', '厘']
      // 整数金额时后面跟的字符
      let cnInteger = '整'
      // 整型完以后的单位
      let cnIntLast = '元'
      // 最大处理的数字
      let maxNum = 999999999999999.9999
      // 金额整数部分
      let integerNum
      // 金额小数部分
      let decimalNum
      // 输出的中文金额字符串
      let chineseStr = ''
      // 分离金额后用的数组，预定义
      let parts
      let money = this.sm
      if (money === '') { return '' }
      money = parseFloat(money)
      if (money >= maxNum) {
        // 超出最大处理数字
        return ''
      }
      if (money === 0) {
        chineseStr = cnNums[0] + cnIntLast + cnInteger
        return chineseStr
      }
      // 转换为字符串
      money = money.toString()
      if (money.indexOf('.') === -1) {
        integerNum = money
        decimalNum = ''
      } else {
        parts = money.split('.')
        integerNum = parts[0]
        decimalNum = parts[1].substr(0, 4)
      }
      // 获取整型部分转换
      if (parseInt(integerNum, 10) > 0) {
        let zeroCount = 0
        let IntLen = integerNum.length
        for (let i = 0; i < IntLen; i++) {
          let n = integerNum.substr(i, 1)
          let p = IntLen - i - 1
          let q = p / 4
          let m = p % 4
          if (n === '0') {
            zeroCount++
          } else {
            if (zeroCount > 0) {
              chineseStr += cnNums[0]
            }
            // 归零
            zeroCount = 0
            chineseStr += cnNums[parseInt(n)] + cnIntRadice[m]
          }
          if (m === 0 && zeroCount < 4) {
            chineseStr += cnIntUnits[q]
          }
        }
        chineseStr += cnIntLast
      }
      // 小数部分
      if (decimalNum !== '') {
        let decLen = decimalNum.length
        for (let i = 0; i < decLen; i++) {
          let n = decimalNum.substr(i, 1)
          if (n !== '0') {
            chineseStr += cnNums[Number(n)] + cnDecUnits[i]
          }
        }
      }
      if (chineseStr === '') {
        chineseStr += cnNums[0] + cnIntLast + cnInteger
      } else if (decimalNum === '') {
        chineseStr += cnInteger
      }
      this.bm = chineseStr
    }
  }

}

</script>

<style scoped>
  #convertM {
    font-family: 'Avenir', Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-align: center;
    color: #2c3e50;
    margin-top: 60px;
  }
.lab_c{
  display: inline-block;
  min-width: 210px;
  line-height: 40px;
  height: 40px;
  text-align: right;
  font-size: 18px;
}
  .in_c {
    width: 280px;
    background: #FFF;
    border: 1px solid #333;
    height: 40px;
    line-height: 40px;
    padding: 0 10px;
    text-align: left;
    font-size: 20px;
  }
  #bm{
    width: 420px;
  }
  #cb{
    background: cadetblue;
    border: 1px solid #333;
    line-height: 40px;
    width: 136px;
    height: 40px;
    font-size: 14px;
  }
  .explain{
    width:34%;
    margin-top: 20px;
    margin-left: auto;
    margin-right: auto;
    text-align: left;
  }
  h4{
    margin-top: 5px;
    margin-bottom: 5px;
  }
  .red-c{
    color:red;
  }
</style>
